package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class DenluActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText yonghu;
    private EditText mima;
    private Button denlu;
    private TextView zhuce;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.yhzhuc).setOnClickListener(this);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        yonghu = findViewById(R.id.yonghui);
        mima = findViewById(R.id.mima);
        denlu = findViewById(R.id.denlu);
        zhuce = findViewById(R.id.yhzhuc);
    }

    private void initData() {
        String url ="http://anli-wechat.boruiman.com/freshmenapp/user/login";
        String nameStr = yonghu.getText().toString().trim();
        String pawwwordStr = mima.getText().toString().trim();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name",nameStr);
            jsonObject.put("password",pawwwordStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestQueue requestQueue = Volley.newRequestQueue(DenluActivity.this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e(TAG, "onResponse: "+jsonObject.optString("msg"));
                Toast.makeText(DenluActivity.this,jsonObject.optString("msg"),Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(DenluActivity.this,"网络失败",Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    private void initEvent() {
        denlu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(DenluActivity.this, ZhuceActivity.class);
        startActivity(intent);
    }
}